use glam::Vec2;
use rand::{self, Rng};
use ggez::{Context, graphics};
use ggez::mint::Point2;

const WIDTH: f32 = 800.0;
const HEIGHT: f32 = 600.0;

pub struct Mover {
    location: Vec2,
    velocity: Vec2
}

impl Mover {
    pub fn new() -> Mover {
        let mut rng = rand::thread_rng();
        let location = Vec2{x: rng.gen_range(0.0..WIDTH), y: rng.gen_range(0.0..HEIGHT)};
        let velocity = Vec2{x: rng.gen_range(-2.0..2.0), y: rng.gen_range(-2.0..2.0)};
        Mover { location, velocity }
    }

    pub fn update(&mut self) -> () {
        self.location += self.velocity;
    }

    pub fn check_edges(&mut self) -> () {
        if self.location.x > WIDTH { self.location.x = 0.0}
        else if self.location.x < 0.0 {self.location.x = WIDTH}

        if self.location.y > HEIGHT { self.location.y = 0.0}
        else if self.location.y < 0.0 {self.location.y = HEIGHT}
    }

    pub fn display(&self, ctx: &mut Context, canvas : &mut graphics::Canvas) -> () {
        let circle = graphics::Mesh::new_circle(
            ctx,
            graphics::DrawMode::fill(),
            Point2{x: self.location.x, y: self.location.y},
            16.0,
            0.1,
            graphics::Color::new(0.5, 0.5, 0.5, 1.0),
        ).unwrap();
        canvas.draw(&circle, graphics::DrawParam::default());
        let circle = graphics::Mesh::new_circle(
            ctx,
            graphics::DrawMode::stroke(2.0),
            Point2{x: self.location.x, y: self.location.y},
            16.0,
            0.1,
            graphics::Color::BLACK,
        ).unwrap();
        canvas.draw(&circle, graphics::DrawParam::default());
    }
}